package com.ksd.dao.impl;

import com.ksd.dao.ISmsDAO;
import com.ksd.pojo.Sms;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import java.util.List;

public class SmsDAO implements ISmsDAO {

    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    /**
     * when have pageSize and pageNo parameters
     */
    public List findAllByUsername(String username) {
        Session session = sessionFactory.getCurrentSession();
        Criteria criteria = session.createCriteria(Sms.class);
        criteria.add(Restrictions.eq("username", username));
        criteria.addOrder(Order.desc("sendtime"));
        return criteria.list();
    }

    public Sms findById(String id) {
        return (Sms) sessionFactory.getCurrentSession().get(Sms.class,
                new Integer(id));
    }

    public void insert(Sms sms) {
        sessionFactory.getCurrentSession().save(sms);
    }

    public void update(Sms sms) {
        sessionFactory.getCurrentSession().update(sms);
    }

    public void delete(String id) {
        Object p = sessionFactory.getCurrentSession().load(Sms.class, new Integer(id));
        sessionFactory.getCurrentSession().delete(p);
    }

}
